package net.bezrabotnih.server.domain;

import java.util.Date;

import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@RooJavaBean
@RooToString
@RooEntity(identifierType = String.class,identifierField="id")
public class LoginAction {

	private String userAccountId;
	
	private Date date;
	
	@Transactional
	public static boolean hasLoggedInFirstTime(){
		 UserService userService=UserServiceFactory.getUserService();
	     User user=userService.getCurrentUser();
	     UserAccount userAccount=UserAccount.findUserAccountByEmail(user.getEmail());
	     return userAccount==null;
	     
	}
	
	@Transactional
	private static long loginActionNum(UserAccount userAccount){
		  return ((Number) entityManager().createQuery("SELECT COUNT(o) FROM LoginAction o WHERE o.userAccountId = :accountId").setParameter("accountId", userAccount.getId()).getSingleResult()).longValue();
	}
	
}
